package com.feditory.category.provider.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.feditory.category.domain.Category;
import com.feditory.category.domain.CategoryChannelType;
import com.feditory.category.domain.CategoryType;
import com.feditory.category.provider.CategoryProvider;
import com.feditory.common.domain.DataHandleStatus;
import com.feditory.util.CollectionUtils;

public class CategoryProviderImpl implements CategoryProvider {

    @PersistenceContext
    private EntityManager em;

    @Override
    public long createCategory(Category category) throws RuntimeException {
        if (category == null) throw new RuntimeException("카테고리 정보가 입력되지 않았습니다.");
        category.setHandleStatus(DataHandleStatus.getNewDataHandler());
        this.em.persist(category);
        return category.getCategoryId();
    }

    //    @Override
    //    public Category retrieveCategory(long categoryId) throws RuntimeException {
    //        if (categoryId < 1) throw new RuntimeException("카테고리 아이디가 입력되지 않았습니다");
    //        Category category = this.em.find(Category.class, categoryId);
    //        if (!category.getHandleStatus().isUseYn()) {
    //            throw new RuntimeException("죽어있는 카테고리 입니다.");
    //        }
    //        return category;
    //    }

    @Override
    public List<Category> retrieveMainCategoryAllList(CategoryChannelType categoryChannelType, CategoryType categoryType) {
        StringBuilder qlString = new StringBuilder(100);
        qlString.append(" SELECT c FROM Category c ");
        qlString.append("  WHERE c.handleStatus.useYn = true ");
        qlString.append("    AND c.categoryType = :categoryType ");
        qlString.append("    AND c.categoryChannelType = :categoryChannelType ");
        qlString.append(" ORDER BY c.displayOrder ");

        Query query = this.em.createQuery(qlString.toString());
        query.setParameter("categoryChannelType", categoryChannelType);
        query.setParameter("categoryType", categoryType);
        return CollectionUtils.castList(query.getResultList());
    }

    @Override
    public List<Category> retrieveCategoryListByParentId(long parentId) {
        StringBuilder qlString = new StringBuilder(100);
        qlString.append(" SELECT c FROM Category c ");
        qlString.append(" WHERE c.handleStatus.useYn = true ");
        qlString.append("   AND c.parentCategoryId = :parentId ");
        // qlString.append("   AND c.categoryType = :categoryType ");
        qlString.append(" ORDER BY c.displayOrder ");

        Query query = this.em.createQuery(qlString.toString());
        query.setParameter("parentId", parentId);
        // query.setParameter("categoryType", CategoryType.SUB);
        return CollectionUtils.castList(query.getResultList());
    }

    //    @Override
    //    public void disableCategory(long categoryId) throws RuntimeException {
    //        if (categoryId < 1) throw new RuntimeException("카테고리 아이디가 입력되지 않았습니다");
    //
    //        Category category = this.em.find(Category.class, categoryId);
    //        category.disable();
    //        this.em.merge(category);
    //
    //    }

    //    @Override
    //    public void disableCategoryList(List<Long> categoruIdList) {
    //        if (CollectionUtils.isEmpty(categoruIdList)) throw new RuntimeException("카테고리 아이디가 입력되지 않았습니다");
    //        for (long categoryId : categoruIdList) {
    //            this.disableCategory(categoryId);
    //        }
    //    }

}
